package com.wang.myapplication.fragment;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wang.myapplication.R;
import com.wang.myapplication.activity.MyHttpback;
import com.wang.myapplication.base.BaseFragment;
import com.wang.myapplication.base.RecyclerBaseAdapter;
import com.wang.myapplication.network.HttpRequest;
import com.wang.myapplication.network.bean.BBSListsResponse;
import com.wang.myapplication.network.bean.BBSListsResponse.Data;

import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * create an instance of this fragment.
 */
public class BBSFragment extends BaseFragment {
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    private RecyclerBaseAdapter<Data> mAdapter;
    private int page;

    @Override
    public int bindLayout() {
        return R.layout.fragment_bbs;
    }

    @Override
    public void initView(View view) {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                reqBBSLists(true);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                reqBBSLists(false);
            }
        });

        refreshLayout.autoRefresh();
        mAdapter = new RecyclerBaseAdapter<Data>(R.layout.lviewitem_bbsdatalist) {
            @Override
            public void handItemView(final MyBaseViewHolder holder, Data pData, int pPosition) {
                ImageView imgIcon = holder.findViewById(R.id.imgIcon);
                TextView tvUsername = holder.findViewById(R.id.tvUsername);
                TextView tvContent = holder.findViewById(R.id.tvContent);
                TextView tvTime = holder.findViewById(R.id.tvTime);
                TextView tvLookNum = holder.findViewById(R.id.tvLookNum);
                TextView tvLikeNum = holder.findViewById(R.id.tvLikeNum);
                TextView tvCommentNum = holder.findViewById(R.id.tvCommentNum);
                TextView tvCommentUser = holder.findViewById(R.id.tvCommentUser);

                RecyclerView rViewImgs = holder.findViewById(R.id.rViewImgs);
                ListView lvComment = holder.findViewById(R.id.lvComment);

                Glide.with(mContext).load(pData.getIconImg()).into(imgIcon);
                tvUsername.setText(pData.getUsername());
                tvUsername.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mActivity, "我是子项目" + v.getId()+"第几个位置："+holder.getPosition(), Toast.LENGTH_SHORT).show();
                    }
                });

                tvContent.setText(pData.getContent());
                tvTime.setText(pData.getTime());
                tvLookNum.setText(pData.getViewNum() + "");
                tvLikeNum.setText(pData.getLikelist().size() + "");
                tvCommentNum.setText(pData.getCommentNum() + "");

                List<String> imgLists = pData.getImg();
                GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                rViewImgs.setLayoutManager(layoutManager);
                rViewImgs.addItemDecoration(new DividerItemDecoration(mContext, layoutManager.getOrientation()));
                rViewImgs.setAdapter(new RecyclerBaseAdapter<String>(imgLists) {
                    @Override
                    public void handItemView(MyBaseViewHolder holder, String pS, int pPosition) {
                        ImageView imageView = (ImageView) holder.itemView;
                        Glide.with(mContext).load(pS).into(imageView);
                    }

                    @Override
                    public MyBaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                        return new MyBaseViewHolder(new ImageView(mContext));
                    }

                });


            }
        };

        //recyclerView使用
        //1、layoutManager
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);
        //2、间隔线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, layoutManager.getOrientation()));
        //3、setAdapter
        mRecyclerView.setAdapter(mAdapter);


        mAdapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object object = v.getTag();
                Toast.makeText(mActivity, "点击了" + v.getTag() + v.getId(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void doBusiness(Context mContext) {
    }

    @Override
    public void widgetClick(View v) {

    }


    private void reqBBSLists(final boolean isRefresh) {
        int classid = 0;
        int typeid = 1;
        String key = "";
        int classtype = 1;

        if (isRefresh)
            page = 1;
        else
            page++;

        HttpRequest.reqGetbbsAllDatas(new MyHttpback<BBSListsResponse>() {
            @Override
            public void onSuccessBack(BBSListsResponse obj) {
                List<Data> dataLists = obj.transferDatalists(obj);
                if (isRefresh)
                    mAdapter.replaceDataList(dataLists);
                else
                    mAdapter.addDataLists(dataLists);
                completeRefresh(isRefresh);
            }
        }, page, classid, typeid, key, classtype);


    }

    private void completeRefresh(boolean isRefresh) {
        if (isRefresh)
            refreshLayout.finishRefresh();
        else
            refreshLayout.finishLoadmore();
    }

}

